package com.skyline.common.listener;

import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import com.skyline.user.model.OnlineUsers;
import com.skyline.user.model.User;

/**
 * Application Lifecycle Listener implementation class OnlineUserCounter
 * 
 */
//TODO：改注释
public class OnlineUserCounter implements HttpSessionAttributeListener,
		HttpSessionListener {

	/**
	 * Default constructor.
	 */
	public OnlineUserCounter() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpSessionAttributeListener#attributeAdded(HttpSessionBindingEvent)
	 */
	public void attributeAdded(HttpSessionBindingEvent hse) {
		if (hse.getName().equals("user")) {
			User user = (User) hse.getValue();
//			OnlineUsers.login(hse.getSession().getId(), u.getUserid(),
//					u.getEmail());
			System.out.println("用户信息:用户" + user.getEmail() + "上线");
		}
	}

	/**
	 * @see HttpSessionAttributeListener#attributeReplaced(HttpSessionBindingEvent)
	 */
	public void attributeReplaced(HttpSessionBindingEvent hse) {
//		if (hse.getName().equals("user")) {
//			User u = (User) hse.getValue();
//			OnlineUsers.relogin(hse.getSession().getId(), u.getId(),
//					u.getUsername());
//			System.out.println("用户信息:session--" + hse.getSession().getId()
//					+ "更换用户.用户" + u.getUsername() + "上线");
//		}
	}

	/**
	 * @see HttpSessionAttributeListener#attributeRemoved(HttpSessionBindingEvent)
	 */
	public void attributeRemoved(HttpSessionBindingEvent hse) {
//		if (hse.getName().equals("user")) {
//			User u = (User) hse.getValue();
//			OnlineUsers.logout(hse.getSession().getId());
//			System.out.println("用户信息:用户" + u.getUsername() + "下线");
//		}
	}

	/**
	 * @see HttpSessionListener#sessionCreated(HttpSessionEvent)
	 */
	public void sessionCreated(HttpSessionEvent hse) {
		System.out.println("用户信息:session--" + hse.getSession().getId() + "连接!");
	}

	/**
	 * @see HttpSessionListener#sessionDestroyed(HttpSessionEvent)
	 */
	public void sessionDestroyed(HttpSessionEvent hse) {
		OnlineUsers.logout(hse.getSession().getId());
		System.out.println("用户信息:session--" + hse.getSession().getId() + "断开!");
	}

}
